home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2006 March / PCWMAR06.iso / Software / Freeware / Thingamablog 1.0.5 / thinga-setup-1.0.5.exe / tb_legacy.jar / BlogPageWriter.java < prev    next >
Encoding:
Java Source  |  2004-03-20  |  13.9 KB  |  371 lines

  1. /*
  2.  * Copyright (C) 2003  Bob Tantlinger
  3.  *
  4.  * This program is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU General Public License
  6.  * as published by the Free Software Foundation; either version 2
  7.  * of the License, or (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  *
  18.  */
  19.  
  20. import java.io.*;
  21. import java.text.*;
  22. import java.util.*;
  23. //import java.sql.*;
  24.  
  25. public class BlogPageWriter extends PrintWriter
  26. {    
  27.     public static int CAT_OR_ARC = -1;
  28.     public static int INDEX = -2;
  29.     public static int FRONT_PAGE = -3;
  30.     private int pageType;
  31.     
  32.     //Tags that work anywhere
  33.     public static final String BLOG_URL = "<$FrontPageLink$>";
  34.     public static final String RSS_FILE = "<$RssLink$>";
  35.     public static final String BLOG_TITLE = "<$BlogTitle$>";
  36.     public static final String BLOG_DESC = "<$BlogDescription$>";
  37.     public static final String ARC_INDEX = "<$IndexPageLink$>";
  38.     public static final String PAGE_TITLE = "<$PageTitle$>"; //unique
  39.     
  40.     public static final String ARCS_BEGIN = "<ArchiveList>";
  41.     public static final String ARC_LINK = "<$ArchiveLink$>";
  42.     public static final String ARC_NAME = "<$ArchiveName$>";
  43.     public static final String ARCS_END = "</ArchiveList>";
  44.     
  45.     //unique to Thingamablog
  46.     public static final String CATS_BEGIN = "<CategoryList>";
  47.     public static final String CAT_LINK = "<$CategoryLink$>";
  48.     public static final String CAT_NAME = "<$CategoryName$>";
  49.     public static final String CATS_END = "</CategoryList>";        
  50.     
  51.     //Tags that only have meaning between <Blogger> and </Blogger>
  52.     //This would be an entry template
  53.     public static final String BLOG_ENTRIES_BEGIN = "<BlogEntry>";
  54.     
  55.     public static final String DAY_HEADER_BEGIN = "<DayHeader>";
  56.     public static final String DAY_HEADER_DATE = "<$DayHeaderDate$>";
  57.     public static final String DAY_HEADER_END = "</DayHeader>";        
  58.     public static final String DAY_FOOTER_BEGIN = "<DayFooter>";
  59.     public static final String DAY_FOOTER_END = "</DayFooter>";
  60.     
  61.     public static final String ENTRY_BODY = "<$EntryBody$>";
  62.     public static final String ENTRY_ID = "<$EntryID$>";
  63.     public static final String ENTRY_AUTHOR = "<$EntryAuthor$>";
  64.     public static final String ENTRY_AUTHOR_EMAIL = "<$EntryAuthorEmail$>";
  65.     public static final String ENTRY_AUTHOR_URL = "<$EntryAuthorURL$>";
  66.     public static final String ENTRY_DATE = "<$EntryDate$>";//unique
  67.     public static final String ENTRY_TIME = "<$EntryTime$>";//unique
  68.     public static final String ENTRY_DATE_TIME = "<$EntryDateTime$>";
  69.     public static final String ENTRY_ARCPAGE = "<$EntryArchivePage$>";
  70.     
  71.     public static final String ENTRY_TITLE_BEGIN = "<EntryTitle>";
  72.     public static final String ENTRY_TITLE = "<$EntryTitle$>";
  73.     public static final String ENTRY_TITLE_END = "</EntryTitle>";
  74.     
  75.     public static final String ENTRY_CATS_BEGIN = "<EntryCategories>";//unique
  76.     public static final String ENTRY_CATS_END = "</EntryCategories>";//unique
  77.     
  78.     //unique
  79.     public static final String ENTRY_MODIFIED_BEGIN = "<EntryModifiedDate>";
  80.     public static final String ENTRY_MODIFIED_DATE = "<$EntryModifiedDate$>";
  81.     public static final String ENTRY_MODIFIED_END = "</EntryModifiedDate>";
  82.     
  83.         
  84.     public static final String BLOG_ENTRIES_END = "</BlogEntry>";
  85.     
  86.     public static synchronized String convertBloggerTemplate(String tmpl)
  87.     {
  88.         tmpl = replaceVariable("<$BlogURL$>", BLOG_URL, tmpl);
  89.         tmpl = replaceVariable("<$BlogTitle$>", BLOG_TITLE, tmpl);
  90.         tmpl = replaceVariable("<$BlogDescription$>", BLOG_DESC, tmpl);
  91.         tmpl = replaceVariable("<$BlogArchiveFileName$>", ARC_INDEX, tmpl);
  92.         tmpl = replaceVariable("<BloggerArchives>", ARCS_BEGIN, tmpl);
  93.         tmpl = replaceVariable("<$BlogArchiveLink$>", ARC_LINK, tmpl);
  94.         tmpl = replaceVariable("<$BlogArchiveName$>", ARC_NAME, tmpl);
  95.         tmpl = replaceVariable("</BloggerArchives>", ARCS_END, tmpl);
  96.         
  97.         tmpl = replaceVariable("<Blogger>", BLOG_ENTRIES_BEGIN, tmpl);
  98.         tmpl = replaceVariable("<BlogDateHeader>", DAY_HEADER_BEGIN, tmpl);
  99.         tmpl = replaceVariable("<$BlogDateHeaderDate$>", DAY_HEADER_DATE, tmpl);
  100.         tmpl = replaceVariable("</BlogDateHeader>", DAY_HEADER_END, tmpl);
  101.         tmpl = replaceVariable("<BlogDateFooter>", DAY_FOOTER_BEGIN, tmpl);
  102.         tmpl = replaceVariable("</BlogDateFooter>", DAY_FOOTER_END, tmpl);
  103.         tmpl = replaceVariable("<$BlogItemBody$>", ENTRY_BODY, tmpl);
  104.         tmpl = replaceVariable("<$BlogItemNumber$>", ENTRY_ID, tmpl);
  105.         tmpl = replaceVariable("<$BlogItemAuthor$>", ENTRY_AUTHOR, tmpl);
  106.         tmpl = replaceVariable("<$BlogItemAuthorEmail$>", ENTRY_AUTHOR_EMAIL, tmpl);
  107.         tmpl = replaceVariable("<$BlogItemAuthorURL$>", ENTRY_AUTHOR_URL, tmpl);
  108.         tmpl = replaceVariable("<$BlogItemDateTime$>", ENTRY_DATE_TIME, tmpl);
  109.         tmpl = replaceVariable("<$BlogItemArchiveFileName$>", ENTRY_ARCPAGE, tmpl);
  110.         tmpl = replaceVariable("<BlogItemTitle>", ENTRY_TITLE_BEGIN, tmpl);
  111.         tmpl = replaceVariable("<$BlogItemTitle$>", ENTRY_TITLE, tmpl);
  112.         tmpl = replaceVariable("</BlogItemTitle>", ENTRY_TITLE_END, tmpl);
  113.         tmpl = replaceVariable("</Blogger>", BLOG_ENTRIES_END, tmpl);                
  114.         
  115.         return tmpl;    
  116.     }    
  117.     
  118.     BlogVariables vals;
  119.     String template;
  120.     String catListTemplate, arcListTemplate, entryTemplate;
  121.     String entryTitleTemplate, entryCatsTemplate, entryModifiedTemplate;
  122.     String dayHeaderTemplate, dayFooterTemplate;
  123.     String pageHeader = "", pageFooter = "";
  124.     int counter;
  125.     int day;
  126.     File archiveDir;
  127.     
  128.     BlogEntry prevEntry;
  129.     String prevArcPage, prevDayHeader, prevDayFooter;
  130.     
  131.     public BlogPageWriter(FileWriter fw, File arcDir, 
  132.     BlogVariables bv, String tmp, String title, int type)
  133.     {
  134.         super(fw);
  135.         pageType = type;
  136.         archiveDir = arcDir;
  137.         vals = bv;
  138.         template = tmp;
  139.         template = replaceVariable(BLOG_TITLE, vals.getTitle(), template);
  140.         template = replaceVariable(BLOG_DESC, vals.getDescription(), template);        
  141.         template = replaceVariable(BLOG_URL, 
  142.             vals.getBaseUrl() + vals.getMainPageFileName(), template);
  143.         template = replaceVariable(ARC_INDEX, 
  144.             vals.getBaseUrl() + vals.getIndexPageFileName(), template);
  145.         template = replaceVariable(RSS_FILE, 
  146.             vals.getBaseUrl() + vals.getRssFileName(), template);
  147.         template = replaceVariable(PAGE_TITLE, title, template);
  148.         
  149.         //write the lists
  150.         while(template.indexOf(CATS_BEGIN) > 0 && template.indexOf(CATS_END) > 0)
  151.         {
  152.             catListTemplate = parseSubTemplate(CATS_BEGIN, CATS_END, template);
  153.             String catList = buildCatList(vals.getCategories(), catListTemplate);
  154.             template = replaceVariable(
  155.                 CATS_BEGIN + catListTemplate + CATS_END, catList, template);
  156.         }
  157.         
  158.         while(template.indexOf(ARCS_BEGIN) > 0 && template.indexOf(ARCS_END) > 0)
  159.         {
  160.             arcListTemplate = parseSubTemplate(ARCS_BEGIN, ARCS_END, template);
  161.             String arcList = buildArcList(arcListTemplate);
  162.             template = replaceVariable(
  163.                 ARCS_BEGIN + arcListTemplate + ARCS_END, arcList, template);
  164.         }
  165.         
  166.         if(pageType == INDEX)
  167.             return;        
  168.         
  169.         entryTemplate = parseSubTemplate(BLOG_ENTRIES_BEGIN, BLOG_ENTRIES_END, template);
  170.         dayHeaderTemplate = parseSubTemplate(DAY_HEADER_BEGIN, DAY_HEADER_END, entryTemplate);
  171.         dayFooterTemplate = parseSubTemplate(DAY_FOOTER_BEGIN, DAY_FOOTER_END, entryTemplate);
  172.         entryTitleTemplate = parseSubTemplate(ENTRY_TITLE_BEGIN, ENTRY_TITLE_END, entryTemplate);
  173.         entryCatsTemplate = parseSubTemplate(ENTRY_CATS_BEGIN, ENTRY_CATS_END, entryTemplate);
  174.         entryModifiedTemplate = parseSubTemplate(ENTRY_MODIFIED_BEGIN, ENTRY_MODIFIED_END, entryTemplate);
  175.                 
  176.         counter = 0;                
  177.     }
  178.     
  179.     public void writeEntry(BlogEntry be, String arcPage) throws IOException
  180.     {        
  181.         if(pageType == INDEX)
  182.             return;
  183.         
  184.         if(counter == 0)
  185.         {
  186.             if((template.indexOf(BLOG_ENTRIES_BEGIN) < 0) || 
  187.                 (template.indexOf(BLOG_ENTRIES_END) < 0))
  188.                 return;
  189.             pageHeader = template.substring(0, template.indexOf(BLOG_ENTRIES_BEGIN));
  190.             pageFooter = template.substring(
  191.                 template.indexOf(BLOG_ENTRIES_END) + BLOG_ENTRIES_END.length(), 
  192.                 template.length() - 1);
  193.             println(pageHeader);                            
  194.         }
  195.         
  196.         String dFooter = "", dHeader = "";
  197.         Calendar cal = Calendar.getInstance();
  198.         cal.setTime(new Date(be.getHeaderData().getTimestamp().getTime()));
  199.         boolean dayChanged = false;
  200.         if(cal.get(Calendar.DAY_OF_YEAR) != day)
  201.         {
  202.             if(counter > 0)
  203.                 dFooter = dayFooterTemplate;
  204.             dHeader = replaceVariable(
  205.                 DAY_HEADER_DATE, vals.getDateFormat().format(be.getTimestamp()), 
  206.                 dayHeaderTemplate);
  207.             day = cal.get(Calendar.DAY_OF_YEAR);
  208.             dayChanged = true;            
  209.         }        
  210.         
  211.         if(counter > 0)
  212.         {
  213.             if(dayChanged)
  214.             {
  215.                 writeOut(prevEntry, prevArcPage, prevDayHeader, dFooter);                    
  216.             }
  217.             else
  218.                 writeOut(prevEntry, prevArcPage, prevDayHeader, "");    
  219.         }
  220.         
  221.         counter++;
  222.         prevEntry = be;
  223.         prevArcPage = arcPage;
  224.         prevDayHeader = dHeader;
  225.         prevDayFooter = dFooter;        
  226.     } 
  227.     
  228.     protected void writeOut(BlogEntry be, String arcPage, String dHeader, String dFooter) throws IOException
  229.     {
  230.         SimpleDateFormat tf = vals.getTimeFormat();
  231.         SimpleDateFormat df = vals.getDateFormat();        
  232.         String time = tf.format(be.getTimestamp());
  233.         String date = df.format(be.getTimestamp());
  234.         String dateTime = date + ' ' + time;
  235.         Author author = vals.getAuthor(be.getAuthor());
  236.         if(author == null)author = new Author();
  237.         
  238.         String eTmp = new String(entryTemplate);
  239.         
  240.         String entryTitle = "";
  241.         if(be.getTitle() != null || !be.getTitle().equals(""))        
  242.             entryTitle = replaceVariable(ENTRY_TITLE, be.getTitle(), entryTitleTemplate);
  243.         
  244.         
  245.         String modDate = "";
  246.         if(be.getLastModified() != null)
  247.             modDate = replaceVariable(ENTRY_MODIFIED_DATE,
  248.                 df.format(be.getLastModified()) + " " + tf.format(be.getLastModified()),
  249.                 entryModifiedTemplate);
  250.         
  251.         String catList = buildCatList(be.getCategories(), entryCatsTemplate);
  252.         
  253.         eTmp = replaceVariable(ENTRY_CATS_BEGIN + entryCatsTemplate + ENTRY_CATS_END, catList, eTmp);            
  254.         eTmp = replaceVariable(ENTRY_TITLE_BEGIN + entryTitleTemplate + ENTRY_TITLE_END, entryTitle, eTmp);            
  255.         eTmp = replaceVariable(DAY_FOOTER_BEGIN + dayFooterTemplate + DAY_FOOTER_END, dFooter, eTmp);
  256.         eTmp = replaceVariable(DAY_HEADER_BEGIN + dayHeaderTemplate + DAY_HEADER_END, dHeader, eTmp);
  257.         eTmp = replaceVariable(ENTRY_DATE, date, eTmp);
  258.         eTmp = replaceVariable(ENTRY_TIME, time, eTmp);
  259.         eTmp = replaceVariable(ENTRY_DATE_TIME, dateTime, eTmp);
  260.         eTmp = replaceVariable(ENTRY_MODIFIED_BEGIN + entryModifiedTemplate + ENTRY_MODIFIED_END, modDate, eTmp);
  261.         eTmp = replaceVariable(ENTRY_BODY, be.getEntryText(), eTmp);
  262.         eTmp = replaceVariable(ENTRY_AUTHOR, author.getName(), eTmp);
  263.         eTmp = replaceVariable(ENTRY_AUTHOR_EMAIL, author.getEmailAddress(), eTmp);
  264.         eTmp = replaceVariable(ENTRY_AUTHOR_URL, author.getUrl(), eTmp);
  265.         eTmp = replaceVariable(ENTRY_ID, be.getID() + "", eTmp);
  266.         
  267.         if(pageType == FRONT_PAGE)
  268.             arcPage = (vals.getArchivesUrl().endsWith("/") ? 
  269.                 vals.getArchivesUrl() : 
  270.                 vals.getArchivesUrl() + '/') + arcPage;
  271.         
  272.         eTmp = replaceVariable(ENTRY_ARCPAGE, arcPage, eTmp);
  273.         
  274.         println(eTmp + "\n");    
  275.     }
  276.     
  277.     public void closePage() throws IOException
  278.     {
  279.         if(pageType == INDEX)
  280.             println(template);
  281.         else
  282.         {    
  283.             if(counter > 0)
  284.             {        
  285.                 writeOut(prevEntry, prevArcPage, prevDayHeader, dayFooterTemplate);
  286.                 println(pageFooter);
  287.             }
  288.             else
  289.                 println(replaceVariable(entryTemplate, "No Entries", template));
  290.         }
  291.     }
  292.     
  293.     protected String parseSubTemplate(String openTag, String closeTag, String tmpl)
  294.     {
  295.         StringBuffer sb = new StringBuffer(tmpl);  
  296.         int s = sb.toString().indexOf(openTag);
  297.         if(s < 0)
  298.             return "";
  299.                                 
  300.         int e = s + openTag.length();
  301.         int tempStart = e;
  302.         
  303.         int tempEnd = sb.toString().indexOf(closeTag);
  304.         if(tempEnd < tempStart)
  305.             return "";
  306.         
  307.         return sb.toString().substring(tempStart, tempEnd);     
  308.     }
  309.     
  310.     protected String buildCatList(String cats[], String tmpl)
  311.     {
  312.         String list = "";
  313.         if(cats == null || cats.length == 0) 
  314.             return list;                    
  315.         String arcUrl = vals.getArchivesUrl();
  316.         if(!arcUrl.endsWith("/"))
  317.             arcUrl += "/";
  318.             
  319.         for(int i = 0; i < cats.length; i++)
  320.         {
  321.             CategoryPage cp = new CategoryPage(archiveDir.getAbsolutePath(), cats[i]);
  322.             String temp = tmpl;
  323.             temp = replaceVariable(CAT_LINK, arcUrl + cp.getName(), temp);
  324.             temp = replaceVariable(CAT_NAME, cats[i], temp);
  325.             list += temp;    
  326.         }
  327.         
  328.         return list;
  329.     }
  330.     
  331.     protected String buildArcList(String tmpl)
  332.     {
  333.         String list = "";
  334.         DatedArchivePage daps[] = DatedArchivePage.listDatedArchivePages(archiveDir);
  335.         if(daps == null || daps.length == 0)
  336.             return list;        
  337.         String arcUrl = vals.getArchivesUrl();
  338.         if(!arcUrl.endsWith("/"))
  339.             arcUrl += "/";        
  340.             
  341.         for(int i = 0; i < daps.length; i++)
  342.         {            
  343.             String temp = tmpl;
  344.             temp = replaceVariable(ARC_LINK, arcUrl + daps[i].getName(), temp);
  345.             temp = replaceVariable(ARC_NAME, daps[i].getTextString(), temp);
  346.             list += temp;    
  347.         }
  348.         
  349.         return list;
  350.     }
  351.     
  352.     protected static String replaceVariable(String var, String val, String tmpl)
  353.     {              
  354.         if(var.equals(""))//don't try to process empty templates
  355.              return tmpl;
  356.         
  357.         while(tmpl.indexOf(var) != -1 && !var.equals(val))
  358.         {
  359.             StringBuffer sb = new StringBuffer(tmpl);  
  360.             int s = sb.toString().indexOf(var);            
  361.             int e = s + var.length();
  362.             sb.delete(s, e);
  363.             sb.insert(s, val);
  364.             tmpl = sb.toString();
  365.             
  366.             //System.out.println("Replaced " + var + " with " + val);
  367.         }
  368.                 
  369.         return tmpl;
  370.     }
  371. }